package csu.web.mypetstore.controller;

import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.domain.CartItem;
import csu.web.mypetstore.pojo.*;
import csu.web.mypetstore.service.*;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrdersService orderService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private CartService cartService;
    @Autowired
    private DeliveryOrderService deliveryService;




    @PostMapping("update")
    @Transactional
    public String updateOrder(
            HttpSession session,
            @RequestParam("orderid") String orderId,
            @RequestParam("userid") String userId,
            @RequestParam("totalprice") BigDecimal totalPrice,
            @RequestParam("locale") String locale, // 新增参数
            @RequestParam("billtofirstname") String billToFirstName,
            @RequestParam("billtolastname") String billToLastName,
            @RequestParam("billaddr1") String billAddr1,
            @RequestParam("billaddr2") String billAddr2,
            @RequestParam("billcity") String billCity,
            @RequestParam("billstate") String billState,
            @RequestParam("billzip") Integer billZip, // 改为Integer
            @RequestParam("billcountry") String billCountry,
            @RequestParam("shiptofirstname") String shipToFirstName,
            @RequestParam("shipaddr1") String shipAddr1,
            @RequestParam("shipaddr2") String shipAddr2,
            @RequestParam("shipzip") Integer shipZip, // 改为Integer
            @RequestParam("shipcity") String shipCity,
            @RequestParam("shipstate") String shipState,
            @RequestParam("shiptolastname") String shipToLastName,
            @RequestParam("shipcountry") String shipCountry,
            @RequestParam("courier") String courier,
            @RequestParam("creditcard") String creditCard,
            @RequestParam("exprdate") String exprDate,
            @RequestParam("cardtype") String cardType) { // 确保包含cardtype


         System.out.println("--------------------orderId---------------------");
        // 获取现有订单
        Orders existingOrder = orderService.getOrder(Integer.parseInt(orderId));

        // 更新字段
       // existingOrder.setOrderdate(orderDate);
        // 更新字段
        existingOrder.setTotalprice(totalPrice.doubleValue());
        existingOrder.setLocale(locale); // 设置locale
        existingOrder.setBilltofirstname(billToFirstName);
        existingOrder.setBilltolastname(billToLastName);
        existingOrder.setBilladdr1(billAddr1);
        existingOrder.setBilladdr2(billAddr2);
        existingOrder.setBillcity(billCity);
        existingOrder.setBillstate(billState);
        existingOrder.setBillzip(billZip); // 直接使用Integer
        existingOrder.setBillcountry(billCountry);
        existingOrder.setShiptofirstname(shipToFirstName);
        existingOrder.setShiptolastname(shipToLastName);
        existingOrder.setShipaddr1(shipAddr1);
        existingOrder.setShipaddr2(shipAddr2);
        existingOrder.setShipcity(shipCity);
        existingOrder.setShipstate(shipState);
        existingOrder.setShipzip(shipZip); // 直接使用Integer
        existingOrder.setShipcountry(shipCountry);
        existingOrder.setCreditcard(creditCard);
        existingOrder.setExprdate(exprDate);
        existingOrder.setCourier(courier);
        existingOrder.setCardtype(cardType); // 确保设置卡类型

        // 保存更新
        orderService.updateOrder(existingOrder);

        return "redirect:/cmsOrderForm";
    }

    @PostMapping("delete")
    @Transactional
    public String deleteOrder(@RequestParam("orderId") String orderId) {

        //orderService.shipOrder(orderId);

        orderService.deleteOrder(Integer.parseInt(orderId));
        orderService.deleteOrderDetail(Integer.parseInt(orderId));
        return "redirect:/cmsOrderForm";
    }
    @GetMapping("deliveryGoods")
    @Transactional
    public String deliveryGoods(@RequestParam("orderId") String orderId) {
        System.out.println("----------------------------deliveryGoods orderId:---------------------------------------- " + orderId);
        //发货单表中插入数据
        DeliveryOrder   deliveryOrder = new DeliveryOrder();
        Orders order = orderService.getOrder(Integer.parseInt(orderId));

        deliveryOrder.setOrderid(order.getOrderid());
        deliveryOrder.setUserid(order.getUserid());
        deliveryOrder.setOrderdate(order.getOrderdate());
        deliveryOrder.setTotalprice(order.getTotalprice());
        deliveryOrder.setBilltofirstname(order.getBilltofirstname());
        deliveryOrder.setBilltolastname(order.getBilltolastname());
        deliveryOrder.setBilladdr1(order.getBilladdr1());
        deliveryOrder.setBilladdr2(order.getBilladdr2());
        deliveryOrder.setBillcity(order.getBillcity());
        deliveryOrder.setBillstate(order.getBillstate());
        deliveryOrder.setBillzip(order.getBillzip());
        deliveryOrder.setCreditcard(order.getCreditcard());
        deliveryOrder.setExprdate(order.getExprdate());



        deliveryService.insertOrder(deliveryOrder);


        //减少订单中的库存
        itemService.decreaseItemInventoryInOrder(Integer.parseInt(orderId));


        //删除订单
        orderService.deleteOrder(Integer.parseInt(orderId));


        return "redirect:/cmsOrderForm";
    }
    @GetMapping("receive")
    @Transactional
    public String receiveGoods(@RequestParam("orderid") String orderId) {

        deliveryService.deleteOrder(orderId);
        orderService.deleteOrderDetail(Integer.parseInt(orderId));
         return "redirect:/myPackageForm";
    }
}
